/*=========================================================================
| KCalDAV
|--------------------------------------------------------------------------
| (c) 2009  Kumaran Santhanam
|
| This project is released under the GNU General Public License.
| Please see the file COPYING for more details.
|--------------------------------------------------------------------------
| Configuration and properties dialog
 ========================================================================*/

/*=========================================================================
| INCLUDES
 ========================================================================*/
#include "resource.h"
#include "config.h"
#include "configwidgets.h"

#include <QtCore/QtAlgorithms>
#include <QtGui/QLayout>
#include <QtGui/QLabel>
#include <QtGui/QComboBox>
#include <QtCore/QList>
#include <QtCore/QTextCodec>
#include <QtGui/QLineEdit>
#include <QtGui/QCheckBox>


/*=========================================================================
| NAMESPACE
 ========================================================================*/
using namespace KCal;


/*=========================================================================
| CONSTANTS
 ========================================================================*/


/*=========================================================================
| STATIC METHODS
 ========================================================================*/
ResourceCalDav* ResourceCalDavConfig::getCalDavResource(KRES::Resource* resource) {
    ResourceCalDav *res = dynamic_cast<ResourceCalDav *>( resource );
    if (!res) {
        kDebug() << "invalid resource type";
    }

    return res;
}

CalDavPrefs* ResourceCalDavConfig::getPrefs(ResourceCalDav* res) {
    CalDavPrefs* p = NULL;

    if (res) {
        p = res->prefs();
        if (!p) {
            kDebug() << "CalDAV: res->prefs() returned NULL";
        }
    }

    return p;
}

/*=========================================================================
| CONSTRUCTOR / DESTRUCTOR
 ========================================================================*/
ResourceCalDavConfig::ResourceCalDavConfig( QWidget *parent )
    : KRES::ConfigWidget( parent )
{
    setupUI();
}


/*=========================================================================
| METHODS
 ========================================================================*/
void ResourceCalDavConfig::loadSettings( KRES::Resource *resource ) {
    ResourceCalDav* res = getCalDavResource(resource);
    CalDavPrefs* p = getPrefs(res);
    if (NULL != p) {
        mUrl->setText(p->url());

        mUsername->setText(p->username());
        mRememberPassword->setChecked(p->rememberPassword());
        mPassword->setText(p->password());

        // Set the encoding combo box to the saved encoding.
        // If the saved encoding was not found, use the current encoding.
        int index = mEncoding->findText(p->encoding());
        if (index < 0)
            index = mEncoding->findText(QTextCodec::codecForLocale()->name());
        mEncoding->setCurrentIndex(index);

        mReloadConfig->loadSettings(res);
        mSaveConfig->loadSettings(res);
    }
}

void ResourceCalDavConfig::saveSettings( KRES::Resource *resource ) {
    ResourceCalDav* res = getCalDavResource(resource);
    if (NULL != res) {
        mReloadConfig->saveSettings(res);
        mSaveConfig->saveSettings(res);

        CalDavPrefs* p = getPrefs(res);
        if (NULL != p) {
            p->setUrl(mUrl->text());
            p->setEncoding(mEncoding->currentText());
            p->setUsername(mUsername->text());
            p->setRememberPassword(mRememberPassword->isChecked());
            p->setPassword(mPassword->text());
        }
    }
}

void ResourceCalDavConfig::setupUI() {
    QVBoxLayout *vertical = new QVBoxLayout(this);
    QGridLayout *mainLayout = new QGridLayout(this);

    // URL
    QLabel *label = new QLabel(i18n("URL:"), this);
    mUrl = new QLineEdit(this);
    mainLayout->addWidget(label, 1, 0);
    mainLayout->addWidget(mUrl, 1, 1);

    // Username
    label = new QLabel(i18n("Username:"), this);
    mUsername = new QLineEdit(this);
    mainLayout->addWidget(label, 2, 0);
    mainLayout->addWidget(mUsername, 2, 1);

    // Password
    label = new QLabel(i18n("Password:"), this);
    mPassword = new QLineEdit(this);
    mPassword->setEchoMode(QLineEdit::Password);
    mainLayout->addWidget(label, 3, 0);
    mainLayout->addWidget(mPassword, 3, 1);

    // Encoding
    label = new QLabel(i18n("Encoding:"), this);
    mEncoding = new QComboBox(this);
    QList<QByteArray> codecs = QTextCodec::availableCodecs();
    qSort(codecs);
    for (int i = 0; i < codecs.size(); i++) {
        QByteArray codec = codecs.at(i);
        mEncoding->addItem(codec);
    }
    mainLayout->addWidget(label, 4, 0);
    mainLayout->addWidget(mEncoding, 4, 1, Qt::AlignLeft);

    // Remember password checkbox
    mRememberPassword = new QCheckBox(i18n("Remember password"), this);
    mainLayout->addWidget(mRememberPassword, 5, 1);

    // configs
    QHBoxLayout* horizontal = new QHBoxLayout(this);

    // Reload config
    mReloadConfig = new CalDavReloadConfig(this);
    horizontal->addWidget(mReloadConfig);

    // Save config
    mSaveConfig = new CalDavSaveConfig(this);
    horizontal->addWidget(mSaveConfig);

    // combining layouts
    vertical->addLayout(mainLayout);
    vertical->addLayout(horizontal);
}

// EOF ========================================================================
